package com.aseo.business;

import java.sql.Timestamp;
import java.util.List;
import java.util.Set;

import javax.annotation.Resource;
import javax.validation.ConstraintViolation;

import org.springframework.stereotype.Service;

import com.aseo.exception.ConstraintViolationException;
import com.aseo.model.dao.Dao;
import com.aseo.model.entity.CatalogoReclamoEstado;
import com.aseo.model.entity.Reclamo;

//@Service
public class ReclamoManager {

	@Resource  
	private Dao dao;
	
	public Reclamo newReclamo() {  
		return new Reclamo();  
	}  
	  
	public void persist(Reclamo reclamo) {
		reclamo.setFechaHora(new Timestamp(System.currentTimeMillis()));
		dao.persist(reclamo);
	}
	  
	public List<Reclamo> getReclamos() {  
		final List<Reclamo> list = dao.find(Reclamo.class);  
	    return list;
	}

	public List<Reclamo> getReclamosPadres() {
		final List<Reclamo> list = dao.find("SELECT reclamo " +
				                            "FROM com.aseo.model.entity.Reclamo reclamo " +
				                            "WHERE reclamo.idPadre IS NULL " +
				                            "ORDER BY reclamo.fechaHora DESC");
		return list;
	}
	
	public List<Reclamo> getReclamosNoCerrados() {
		final List<Reclamo> list = dao.find("SELECT reclamo FROM com.aseo.model.entity.Reclamo reclamo WHERE reclamo.idCatalogoReclamoEstado IN (" 
				                   + CatalogoReclamoEstado.ACTIVO.getIdCatalogoReclamoEstado() + ","
				                   + CatalogoReclamoEstado.EN_PROCESO.getIdCatalogoReclamoEstado() + ")" 
				                   + " AND reclamo.idPadre IS NULL ORDER BY reclamo.fechaHora DESC");
		return list;
	}
	
	public List<Reclamo> getReclamosNoCerradosNoPadre() {
		final List<Reclamo> list = dao.find("SELECT reclamo FROM com.aseo.model.entity.Reclamo reclamo WHERE reclamo.idCatalogoReclamoEstado IN (" 
				                   + CatalogoReclamoEstado.ACTIVO.getIdCatalogoReclamoEstado() + ","
				                   + CatalogoReclamoEstado.EN_PROCESO.getIdCatalogoReclamoEstado() + ")" 
				                   + " AND reclamo.idPadre IS NULL " 
				                   + " AND NOT reclamo IN "
				                   + " (SELECT reclamo_hijo.idPadre FROM com.aseo.model.entity.Reclamo reclamo_hijo WHERE NOT reclamo_hijo.idPadre IS NULL) " 
				                   + "  ORDER BY reclamo.fechaHora DESC");
		return list;
	}
	
	public List<Reclamo> getReclamosCandidatos (Reclamo reclamo) {
		final List<Reclamo> list = dao.find("SELECT reclamo FROM com.aseo.model.entity.Reclamo reclamo "
											+ " WHERE (reclamo.idPadre.idReclamo = " + reclamo.getIdReclamo() + " " 
											+ " OR ( reclamo.idPadre IS NULL " 
											+ " AND NOT reclamo IN (SELECT reclamo_hijo.idPadre FROM com.aseo.model.entity.Reclamo reclamo_hijo WHERE NOT reclamo_hijo.idPadre IS NULL)))" 
											+ " AND  reclamo.idParroquia.idParroquia = " + reclamo.getIdParroquia().getIdParroquia()
											+ " AND  reclamo.idSector.idSector = " + reclamo.getIdSector().getIdSector() 
											+ " AND  reclamo.idReclamo <> " + reclamo.getIdReclamo()
											+ " ORDER BY reclamo.idReclamo");
		return list;
	}
	
	public Reclamo get(Long idReclamo) {
		return dao.get(Reclamo.class, idReclamo);
	}
	
	public void validate(Reclamo reclamo) throws ConstraintViolationException {

		Set<ConstraintViolation<Reclamo>> constraintViolations = dao.validate(reclamo);
		
		for (ConstraintViolation<Reclamo> constraintViolation : constraintViolations) {
			throw new ConstraintViolationException(constraintViolation.getMessage());
		}
	}

}